﻿package src
{
	import flash.display.MovieClip;

	public class Spawner extends MovieClip
	{
		private var roca:Roca;
		private var myParent:DefensaKuelap;
		public var pos:Number;
		private var op:uint;
		private var num1:Number;
		private var num2:Number;
		private var n1:Number;
		private var newNum1:Number;
		public function Spawner(par:DefensaKuelap)
		{
			this.myParent = par;
		}
		public function spawn(ramValue):void{
			roca = new Roca();
			myParent.rocas.push(roca);			
			generateOP(myParent.rocas,myParent.rocas.length);
			myParent.addChild(roca);
			
			roca.x = this.x;
			roca.y = this.y + ramValue;
			pos = roca.y;
			
		}
		private function generateOP(array:Array, n:Number):void{
			op = 2; //Math.floor(Math.random()*3 + 1);
			trace(op);			
			
			switch (op){
				//suma
				case 1: num1 = Math.floor(Math.random()*98 + 1);
						num2 = Math.floor(Math.random()*99 + 1);
						array[n-1].operation.text = num1.toString() + '+' + num2.toString();
						break;
				//resta
				case 2: num1 = Math.floor(Math.random()*98 + 1);
						num2 = Math.floor(Math.random()*49 + 1);
						if (num1 > num2)
							array[n-1].operation.text = num1.toString() + '-' +num2.toString();
						else{
							newNum1 = Math.floor(((num1*num2)/100) + (num2-num1)) + num1;
							array[n-1].operation.text = newNum1.toString() + '-' +num2.toString();
						}
						break;
				//multiplicacion
				case 3: num1 = Math.floor(Math.random()*98 + 1);
						num2 = Math.floor(Math.random()*9 + 1);
						array[n-1].operation.text = num1.toString() + '*' + num2.toString();
						break;
				//division
				case 4: num1 = Math.floor(Math.random()*25 + 2);
						n1 = Math.floor(Math.random()*9 + 2);
						num2 = num1 * n;
						if (num2 != num1)
					        array[n-1].operation.text = num2.toString() + '/' + num1.toString();
						else{
							num2 = 3*num1;
							array[n-1].operation.text = num2.toString() + '/' + num1.toString();
							}
						break;
				
			}
		}
		
	}
}